import modelExtend from 'dva-model-extend'
import { parse } from 'qs'
import { pageModel } from './common'
import { searchs,userLis,changeState,remove,infoDetais,userLists,sends } from 'services/search'
import { type,group } from 'services/newfile'

export default modelExtend(pageModel, {
  namespace: 'search',
  state: {
  	sumList:'',
  	showDetails:false,
  	visible:false,
  	info:'',
  	depTypes:'',
  	userList:'',
  	list:'',
  	details:'',
  	params:'',
  	department2:'',
  	depUser:'',
  	infoData:'',
  	pagination1: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
    },
  },
  subscriptions: {
    	setup({dispatch,history}){
    		history.listen((location)=>{
    			if(location.pathname === '/total/search'){
    				dispatch({
    					type:'depType',
    					payload:location.query,
    				})
    				dispatch({
    					type:'useLists',
    					payload:location.query,
						})
						dispatch({
							type:"searchList",
						})
    			}
    		})
    	}
  },
  effects: {
  	*info({payload},{call,put}){
		const data = yield call(infoDetais, payload);	
		yield put({type: 'showModal'});
		if(data){
			yield put({ 
	    			type: 'querySuccessC',
	    			payload: {
		          infoData:data,
		        }
	    		})
		}
  	},
  	*dep({payload},{call,put}){
   		const data = yield call(group,payload)
   		if(data.success){
   			yield put({
   				type:"querySuccessA",
   				payload:{
   					department2:data.list
   				}
   			})
   		}
   	},
   	*users({payload},{call,put}){
   		const data = yield call(userLists,payload)
   		if(data){
   			yield put({
   				type:"querySuccessB",
   				payload:{
   					depUser:data.userList
   				}
   			})
   		}
   	},
  	*update({payload},{call,put}){
	      yield put({ type: 'hideModal' })
	      yield put({ type: 'showLoading' })
//	      const id = yield select(({ contract }) => contract.currentItem.id)
//	      const newContract = { ...payload, id }
	      const data = yield call(sends, payload)
//	      console.log(data)
	      if (data) {
	        yield put({ type: 'reload' });
	      }else{
	        yield put({ type: 'hideLoading' })
	      }  		
  },
  	*'delete' ({ payload }, { call, put }) {
//	      console.log("111")
	      yield put({ type: 'showLoading' })
	      const data = yield call(remove, { id: payload })
	      if (data.success) {
	        yield put({ type: 'reload' });
	      }else{
	        yield put({ type: 'hideLoading' })
	      }
    },
    *complete({ payload },{ call, put }){
    		  yield put({ type: 'showLoading' })
	      const data = yield call(changeState, payload )
	      if (data.success) {
	        yield put({ type: 'reload' });
	      }else{
	        yield put({ type: 'hideLoading' })
	      }
    },
    *reload(action, { put, select }) {
      const page = yield select(({ search }) => search.pagination1)

      yield put({ type: 'searchList', payload: { 
          current:page.current
       } });
    },
   	*showDet({payload},{call,put}){
// 		console.log(payload)
// 		const data = payload
   		const data = yield call (infoDetais, payload)
// 		console.log(data)
    		yield put({ 
    			type: 'querySuccess',
    			payload: {
	          showDetails:true,
	          details:data
	        }
    		})
    		
    },
   	*showLis({payload},{call,put}){
    		yield put({ 
    			type: 'querySuccess',
    			payload: {
	          showDetails:false,
	        }
    		})
    },    
    *depType({payload},{call,put}){
    		const data = yield call (type,payload)
//  		console.log(data.list)
    		if(data.success){
    			yield put({
	    			type:'querySuccess1',
	    			payload:{
	    				depTypes:data.list
	    			}
	    		})
    		}
    },
    *useLists({payload},{call,put}){
    		const data = yield call (userLis,payload)
//  		console.log(data.userList)
    		if(data.success){
    			yield put({
    				type:'querySuccess2',
    				payload:{
    					userList:data.userList
    				}
    			})
    		}
    },
    *searchList({payload},{call,put}){
    		const data = yield call (searchs,payload)
//  		console.log(data)
    		if(data.success){
    			yield put({
    				type:'querySuccess3',
    				payload:{
    					params:payload,
    					list:data.page.list,
    					pagination1:{
    						total: data.page.totalCount,
            				current: data.page.currPage
    					}
    				}
    			})
    		}
    }
  },
  reducers: {
  	showLoading (state) {
      return { ...state, loading: true }
    },
    hideLoading (state) {
      return { ...state, loading: false }
    },
    querySuccess(state, action){
    		const {showDetails,info,details } = action.payload
	      return { ...state,
	        showDetails,
	        info,
	        details,
	      }
    },
    querySuccessA (state, action) {
      const {department2} = action.payload
      return { ...state,
      	department2
      }
    },   
    querySuccessB (state, action) {
      const {depUser} = action.payload
      return { ...state,
      	depUser
      }
    },  
    querySuccessC (state, action) {
      const {infoData} = action.payload
      return { ...state,
      	infoData
      }
    }, 
    querySuccess1(state, action){
    		const {depTypes} = action.payload
    		return {...state,
    			depTypes
    		}
    },
    querySuccess2(state,action){
    		const {userList} = action.payload
    		return {...state,
    			userList
    		}
    },
    querySuccess3(state,action){
    		const {list,pagination1,params} = action.payload
    		return {...state,
    			list,
    			params,
    			pagination1: {
	          ...state.pagination1,
	          ...pagination1
	        }
    		}
    },
    showModal(state, action){
    		return {
    			...state,
    			...action.payload,
    			visible:true,
    		}
    },
    hideModal (state, action){
	      return { ...state, 
	        ...action.payload, 
	        visible: false 
	      }
    },
    onCancel (state, action) {
      return { ...state,
        visible: false,
      }
    },
  },
})
